package se.krutstampen.backend.domain.model.product;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;

/**
 * 
 * Aggregat-root för Product
 * 
 * 
 * @author hans
 * 
 */

@Entity
public class Product {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCT_GEN")
	@SequenceGenerator(name = "PRODUCT_GEN", sequenceName = "PRODUCT_SEQ", allocationSize = 1)
	private Long id;

	@ManyToOne
	private Group group;

	private String name;

	@ManyToMany
	@JoinTable(name = "PRODUCT_FIRMWARE", 
			joinColumns = @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID"), 
			inverseJoinColumns = @JoinColumn(name = "FIRMWARE_ID", referencedColumnName = "ID"))
	private List<Firmware> firmwares;

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Firmware> getFirmwares() {
		return firmwares;
	}

	public void setFirmwares(List<Firmware> firmwares) {
		this.firmwares = firmwares;
	}

	public void addFirmware(Firmware firmware) {
		
		if(firmwares == null){
			firmwares = new ArrayList<Firmware>();
		}
		firmwares.add(firmware);
		firmware.addProduct(this);
	}
	
}
